﻿using System;
using System.Drawing;
using System.IO;
using Jage.Common;

namespace Jage.Logic.Resources
{
    public class ResourceIO
    {

        public static string GetAbsolute(string resourceLocation)
        {
            return string.Format("{0}\\{1}", Path.GetFullPath(Configuration.Default.ResourcePath), resourceLocation);
        }

        public static string GetRelative(string filename)
        {
            string resourcePath = Path.GetFullPath(Configuration.Default.ResourcePath.ToLower());
            if (filename.ToLower().StartsWith(resourcePath))
            {
                return filename.Substring(resourcePath.Length + 1); // also remove last backslash.
            }
            else
            {
                throw new ArgumentException(string.Format("Cannot get relative resourcepath for \"{0}\".  It's outside the resource folder.", filename));
            }
        }

        public static Bitmap GetBitmap(string resource)
        {
            if (File.Exists(GetAbsolute(resource)))
            {
                return new Bitmap(GetAbsolute(resource));
            }
            else
            {
                return null;
            }
        }

    }
}
